<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>求解方程</title>
</head>
<body>
  <script>
    var equation = 'x+5-3+x=6+x-2'
    function fn(equation) {
      let xCount = 0; // x的数量
      let param = 0; // 剩余数值
      let arr = equation.split('=');
      let helper = function (equation, sym) {
        let tempParam = '';
        let symbol = '+'; // 当前符号
        let i = 0;
        equation += '+' // 等式最后加上一个'+'为了处理最后的剩余数值
        while (equation[i]) {
          let char = equation[i];
          if (char === 'x') {
            let temp = parseInt(tempParam || 1)
            tempParam = '';
            symbol === sym ? xCount += temp : xCount -= temp
          } else if (char === '+' || char === '-') {
            let temp = parseInt(tempParam || 0)
            tempParam = '';
            symbol === sym ? param -= temp : param += temp
            symbol = char
          } else {
            tempParam += char;
          }
          ++i;
        }
      }
      helper(arr[0], '+')
      helper(arr[1], '-')
      if (xCount === 0 && param === 0) return 'Infinite solutions'
      if (xCount === 0) return 'No solution'
      return 'x=' + (param / xCount)
    }
    console.log(fn(equation))
  </script>
</body>
</html>